package control

import (
	"antServer/antPublic"
	"io"
	"net/http"
	"strconv"

	"github.com/antlinker/asapi"
)

func init() {
	http.HandleFunc("/api/mqtt/send", mqttsend)
	http.HandleFunc("/api/mqtt/pwd", getmqttpwd)
}
func mqttsend(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()

	qos, _ := strconv.Atoi(r.FormValue("qos"))
	retain, _ := strconv.Atoi(r.FormValue("retain"))
	topic := r.FormValue("topic")
	payload := r.FormValue("payload")
	re := false
	if retain == 1 {
		re = true
	}
	antPublic.Publish(topic, uint8(qos), re, []byte(payload))
	io.WriteString(w, "{\"msg\":\"发送成功\"}")
}
func createPassword() string {
	token, err := asapi.ForceGetToken()
	if err != nil {
		//ALOG.Warn("获取令牌失败:(%d):%s", err.Code, err.Message)
	}
	return "SERVER_" + token
}
func getmqttpwd(w http.ResponseWriter, r *http.Request) {

	password := createPassword()

	io.WriteString(w, "{\"key\":\""+password+"\"}")
}
